NProgress.start();
window.onload = function () {
    NProgress.done();
}

layui.config({
    base: '/lib/layui/lay/modules/'
}).extend({
    treetable: 'treetable/treetable'
}).use(['element', 'table', 'form', 'jquery', 'treetable'], function () {
    var element = layui.element;
    var form = layui.form;
    var table = layui.table;
    var $ = layui.jquery;
    var treetable = layui.treetable;
    layui.renderTree = function (query) {
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            treeDefaultClose: false,//是否默认折叠
            treeLinkage: true,//父级展开时是否自动展开所有子级
            elem: '#tableId',
            url: '/system/menu/list',
            where: query,
            id: 'menuTreeTable',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'name', minWidth: 180, title: '名称'},
                {field: 'url', minWidth: 220, title: 'URL'},
                {
                    field: 'type', width: 80, align: 'center', templet: function (d) {
                        if (d.type == 2) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.type == 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {field: 'perms', minWidth: 280, title: '授权'},
                {
                    field: 'icon', width: 80, title: '图标', templet: function (d) {
                        return d.icon == null ? '' : '<i class="' + d.icon + '"></i>';
                    }
                },
                {field: 'orderNum', title: '排序'},
                {templet: '#operationTpl', width: 240, align: 'center', title: '操作'}
            ]]
            , response: {
                statusCode: true //重新规定成功的状态码为 200，table 组件默认为 0
            }
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.success, //解析接口状态
                    "msg": res.data.errMsg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            done: function (res, curr, count) {
                layer.closeAll('loading');
                $("#countNum").text(count);
            }
        });
    }
    layui.renderTree({});
    form.on('submit(search)', function (data) {
        layui.renderTree(data.field)
        return false;
    });

    table.on('tool(tableFilter)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            operater("编辑菜单", '/system/menu/edit/' + data.id);
        } else if (layEvent === 'add') {
            operater("添加菜单", '/system/menu/add/' + data.id);
        } else if (layEvent === 'del') {
            layer.confirm("确定要删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                $.post("/system/menu/remove", {deptId: data.id}, function (res) {
                    if (res.success) {
                        layer.msg("操作成功！", {icon: 6, time: 700, anim: 4}, function () {
                            layui.renderTree({});
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else {
                        parent.layer.alert(res.data.errMsg);
                    }
                }, 'json');
            });
        }
    });

    var operater = function (title, url) {
        layer.open({
            title: title,
            type: 2,
            shade: false,
            maxmin: false,
            shade: 0.5,
            anim: 4,
            area: ['80%', '80%'],
            content: url,
            zIndex: layer.zIndex,
            end: function () {
                $(".layui-laypage-btn")[0].click();
            }
        });
    }
    $("#addMenu").click(function () {
        operater("添加菜单", "/system/menu/add/0");
    })
})